<script setup lang="ts">
import { ref } from 'vue'
import {
  NForm,
  NFormItem,
  NInputNumber,
  NInput,
  NButton,
  NGrid,
  NGi,
  NTooltip,
  NScrollbar,
  NTab,
  NTabPane,
  NCheckbox,
  NSelect
} from 'naive-ui'
import { invoke } from '@tauri-apps/api'
import SettingService from '@/services/setting.service'
import Foods from './foods.vue'

const settingService = new SettingService()
const hugSetting = ref({ ...settingService.hugConfig })

invoke('toggle_deltool')

function onTypeChange() {
  let cfg = settingService.settings.find(v => v.type === hugSetting.value.type)
  console.log(cfg, settingService.settings)
  if (!cfg) {
    return
  }
  hugSetting.value = JSON.parse(JSON.stringify(cfg))
}
function onSave() {
  settingService.saveHug(hugSetting.value)
}
function onReset() {
  settingService.resetHug()
  hugSetting.value = { ...settingService.hugConfig }
}
</script>

<template>
  <div class="hug">
    <n-scrollbar class="region input">
      <n-form label-width="55" label-placement="left" size="small" :show-feedback="false">
        <n-grid x-gap="12" y-gap="10" :cols="24">
          <n-gi span="8">
            <n-form-item label="练体方式" label-width="68">
              <n-select v-model:value="hugSetting.type" :options="[{value:0,label: '推车练体'}, {value:1,label: '爬行练体'}, {value:2,label: '砸箱子练体'}]" @update:value="onTypeChange" />
            </n-form-item>
          </n-gi>
          <n-gi span="8">
            <n-form-item label="吃喝方式" label-width="68">
              <n-select v-model:value="hugSetting.eat_type" :options="[{value:0,label: '快捷键'}, {value:1,label: '箱子自取'}]" />
            </n-form-item>
          </n-gi>
          <n-gi span="8">
            <n-form-item label="额外参数" label-width="68">
              <n-input-number :show-button="false" v-model:value="hugSetting.sub_type" :precision="0" :min="0" :max="15" />
            </n-form-item>
          </n-gi>
          <n-gi span="7">
          </n-gi>
          <n-gi span="24">
            <div class="group">吃喝配置:</div>
          </n-gi>
          <n-gi span="24">
            <Foods v-model="hugSetting.foods" :eat_type="hugSetting.eat_type" :addable="hugSetting.eat_type==1" />
          </n-gi>
          <n-gi span="24">
            <div class="group">维修配置:</div>
          </n-gi>
          <!-- 修鞋子 -->
          <n-gi span="4">
            <n-form-item label="" label-width="0">
              <n-checkbox  v-model:checked="hugSetting.shoe.enable">鞋子</n-checkbox>
            </n-form-item>
          </n-gi>
          <n-gi span="6">
            <n-form-item label="位置" label-width="45">
              <n-input :disabled="!hugSetting.shoe.enable" :show-button="false" v-model:value="hugSetting.shoe.key" :allow-input="v => !v || /^[56]$/.test(v)" />
            </n-form-item>
          </n-gi>
          <n-gi span="7">
            <n-form-item label="竖向偏移" label-width="68">
              <n-input-number :disabled="true" :show-button="false" v-model:value="hugSetting.shoe.y_offset" :precision="0" :min="0" :max="500" />
            </n-form-item>
          </n-gi>
          <n-gi span="7">
            <n-form-item label="维修间隔" label-width="68">
              <n-input-number :disabled="!hugSetting.shoe.enable" :show-button="false" v-model:value="hugSetting.shoe.interval" :precision="0" :min="1" :max="20" />
            </n-form-item>
          </n-gi>
          <!-- 修手套 -->
          <n-gi span="4">
            <n-form-item label="" label-width="0">
              <n-checkbox  v-model:checked="hugSetting.glove.enable">手套</n-checkbox>
            </n-form-item>
          </n-gi>
          <n-gi span="6">
            <n-form-item label="位置" label-width="45">
              <n-input :disabled="!hugSetting.glove.enable" :show-button="false" v-model:value="hugSetting.glove.key" :allow-input="v => !v || /^[56]$/.test(v)" />
            </n-form-item>
          </n-gi>
          <n-gi span="7">
            <n-form-item label="竖向偏移" label-width="68">
              <n-tooltip trigger="hover">
                <template #trigger>
                  <n-input-number :disabled="!hugSetting.glove.enable" :show-button="false" v-model:value="hugSetting.glove.y_offset" :precision="0" :min="0" :max="500" />
                </template>
                漏指手套 57 大手套 76
              </n-tooltip>
              </n-form-item>
          </n-gi>
          <n-gi span="7">
            <n-form-item label="维修间隔" label-width="68">
              <n-input-number :disabled="!hugSetting.glove.enable" :show-button="false" v-model:value="hugSetting.glove.interval" :precision="0" :min="1" :max="20" />
            </n-form-item>
          </n-gi>
          <n-gi span="7">
            <n-form-item label="缝纫工具位置" label-width="98">
              <n-input-number :disabled="!hugSetting.shoe.enable&&!hugSetting.glove.enable" :show-button="false" v-model:value="hugSetting.toolkits[0]" :precision="0" :min="0" :max="20" />
            </n-form-item>
          </n-gi>
          <n-gi span="3">
            <n-form-item label="" label-width="0">
              <n-input-number :disabled="!hugSetting.shoe.enable&&!hugSetting.glove.enable" :show-button="false" v-model:value="hugSetting.toolkits[1]" :precision="0" :min="0" :max="20" />
            </n-form-item>
          </n-gi>
        </n-grid>
      </n-form>
    </n-scrollbar>
    <div class="actions">
      <n-button type="error" size="small" @click="onReset" style="margin-right:8px">重置</n-button>
      <n-button type="primary" size="small" @click="onSave">保存 & 设置</n-button>
    </div>
  </div>
</template>

<style scoped lang="less">
.hug {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  :deep(.n-scrollbar-content) {
    padding: 0 15px 15px 15px;
  }
  .actions {
    padding: 8px;
    box-shadow: 0 0 1px 0 #DDD;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
